//
//  MZYcollectViewController.m
//  WristWatch
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MZYcollectViewController.h"
#import "DataBaseHandle.h"
#import <UIImageView+WebCache.h>
#import "MZYthirdViewController.h"
#import "MZYstyleModel.h"

@interface MZYcollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UITableView *tableView;

@end

@implementation MZYcollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView release];
    
    self.arr = [NSMutableArray array];
    self.arr = [[DataBaseHandle shareDataBaseHandle] selectAllStyle];
    
    if (self.arr.count == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"没有数据" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        
        
        
    }
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    cell.textLabel.text = [self.arr[indexPath.row] name];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[self.arr[indexPath.row] image]]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MZYthirdViewController *vc = [[MZYthirdViewController alloc] init];
    MZYstyleModel *model = self.arr[indexPath.row];
    
    vc.style = model;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
